const app = getApp();
Component({
  data: {
    sortOptions: [
      { text: '时间', value: 'time' },
      { text: '按年', value: 'year' },
      { text: '按月', value: 'month' },
      { text: '名称', value: 'name' }
    ],
    actions: [
      { name: "编辑", action: 'edit' },
      { name: "删除", color: "#ee0a24", action: 'delete' }
    ],
    actionSheetShow: false,
    depList: [],
    depId: 0,
  },
  lifetimes: {
    attached() {
      this.getDepartmentList()
    }
  },
  methods: {
    getDepartmentList() {
      app.axios('user', {
        op: 'ajaxDepLists'
      }).then(res => {
        if (res.code) {
          if (res.lists && res.lists.length > 0) {
            res.lists.forEach(item => {
              item.creattime = app.dateFormatter(item.addtime)
            })
          }
          this.setData({
            depList: res.lists
          })
        }
      })
    },
    onSelect(event) {
      const id = this.data.depId
      const action = event.detail.action;
      const alert = this.selectComponent('#v-alert')
      if (action === 'edit') {
        app.navigateTo(`/pages/operation-center/add-department/index?id=${id}`)
      } else if (action === 'delete') {
        alert.toggleAlert()
      }
    },
    handleDelete() {
      const id = this.data.depId
      app.axios('user', {
        op: 'ajaxDepDel',
        id: id,
      }).then(res => {
        if (res.code) {
          wx.showToast({ title: '删除成功' })
          setTimeout(() => {
            this.getDepartmentList()
          }, 500);
        }
      })

    },
    onActionSheetCancel() {
      this.setData({
        actionSheetShow: false,
      });
    },
    onActionSheetClose() {
      this.setData({
        actionSheetShow: false,
      });
    },
    onCustomerTap(e) {
      const id = e.currentTarget.dataset.id
      this.setData({
        actionSheetShow: true,
        depId: id
      });
    },
    onAddCustomer() {
      app.navigateTo(`/pages/operation-center/add-department/index`)
    },
    onCall(e) {
      // 拨打电话逻辑
    },
  }
})